import Vue from 'vue'
import Router from 'vue-router'
import Home from '../components/Home'
import Login from '../components/Login'
import Welcome from '../components/Welcome'
import Liscense from '../components/liscense/Liscense'
import AddLiscense from '../components/liscense/AddLiscense'
import Member from '../components/liscense/Member'
import EditLiscense from '../components/liscense/EditLiscense'
import Liscensetab from '../components/liscense/Liscensetab'
import Feedback from '../components/feedback/Feedback'
import AddFeedback from '../components/feedback/AddFeedback'
import EditFeedback from '../components/feedback/EditFeedback'
import Defect from '../components/defect/Defect'
import Adddefect from '../components/defect/Adddefect'
import Editdefect from '../components/defect/Editdefect'
import Etdataadd from '../components/etdata/Etdataadd'
import Etdatasearch from '../components/etdata/Etdatasearch'
import Baseinfo from '../components/baseinfo/Baseinfo'
import Outage from '../components/baseinfo/Outage'
import Changepassword from '../components/personal/Changepassword'


Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            name: 'Home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/liscense', component: Liscense },
                { path: '/addLiscense', component: AddLiscense },
                { path: '/member', component: Member },
                { path: '/editliscense', component: EditLiscense },
                { path: '/liscensetab', component: Liscensetab },
                { path: '/feedback', component: Feedback },
                { path: '/addfeedback', component: AddFeedback },
                { path: '/editfeedback', component: EditFeedback },
                { path: '/defect', component: Defect },
                { path: '/adddefect', component: Adddefect },
                { path: '/editdefect', component: Editdefect },
                { path: '/etdataadd', component: Etdataadd },
                { path: '/etdatasearch', component: Etdatasearch },
                { path: '/baseinfo', component: Baseinfo },
                { path: '/outage', component: Outage },
                { path: '/changepassword', component: Changepassword },
            ]
        }
    ]
})

// 挂载路由守卫, 判断用户是否登录
router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router